
// YYYY-MM-DD
const formatDate = (date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

// 小时:分钟
const hourMinute = (time) => {
  const date = new Date(time);
  // 提取时间部分
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  return `${hours}:${minutes}`;
}
// 计算上个月起止时间
const getLastMonthRange = () => {
  const now = new Date();
  // 获取当前月份
  const currentMonth = now.getMonth();
  // 获取当前年份
  const currentYear = now.getFullYear();

  // 上个月的年份和月份
  const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1;
  const lastMonthYear = currentMonth === 0 ? currentYear - 1 : currentYear;

  // 上个月的第一天
  const startOfLastMonth = new Date(lastMonthYear, lastMonth, 1);
  // 上个月的最后一天
  const endOfLastMonth = new Date(lastMonthYear, lastMonth + 1, 0);

  return {
    start: formatDate(startOfLastMonth),
    end: formatDate(endOfLastMonth)
  };
};

// 本月的起止时间
const getStartAndEndOfMonth = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = now.getMonth();

  // 获取本月第一天
  const startOfMonth = new Date(year, month, 1);

  // 获取下月第一天
  const endOfMonth = new Date(year, month + 1, 0);

  // 设置时间为23:59:59.999
  endOfMonth.setHours(23, 59, 59, 999);

  return {
    start: formatDate(startOfMonth),
    end: formatDate(endOfMonth)
  };
};


export { formatDate, getLastMonthRange, getStartAndEndOfMonth, hourMinute }